<template>
  <div>
    <el-breadcrumb class="app-breadcrumb" separator=">">
      <transition-group name="breadcrumb">
        <el-breadcrumb-item v-for="(item,index) in levelList" v-if="item.meta.title" :key="item.path">
          <svg-icon v-if="item.meta.icon === 'yemian-copy'" :icon-class="item.meta.icon"/>
          <span v-if="item.redirect==='noredirect'||index==levelList.length-1" class="no-redirect">{{ item.meta.title }}</span>
          <router-link v-else :to="item.redirect||item.path">{{ item.meta.title }}</router-link>
          <div v-if="item.meta.title.indexOf('摄像头') !== -1" style="display:inline-block;margin-left: -53px;">
            <el-radio-group v-model="globalCameraStatus" size="mini" class="radio-group-color">
              <el-radio-button label="1">预览</el-radio-button>
              <el-radio-button label="2">回放</el-radio-button>
            </el-radio-group>
          </div>
        </el-breadcrumb-item>
      </transition-group>
    </el-breadcrumb>
    <div class="nowDate-box">
      <div class="blue-btn">
        <i class="el-icon-date" />
        <span v-html="dateString"/>
        <el-popover
          placement="bottom"
          width="400"
          trigger="click">
          <i slot="reference" class="el-icon-arrow-down" />
        </el-popover>
      </div>
    </div>
  </div>
</template>

<script>
import pathToRegexp from 'path-to-regexp'
import { formatDate } from '../../utils/formatDate'

export default {
  data () {
    return {
      levelList: null,
      dateString: formatDate()
    }
  },
  computed: {
    globalCameraStatus: {
      get: function () {
        return this.$store.state.camera.status
      },
      set: function (newValue) {
        this.$store.dispatch('ChangeStatus', newValue)
      }
    }
  },
  watch: {
    $route () {
      this.getBreadcrumb()
    }
  },
  created () {
    this.getBreadcrumb()
  },
  methods: {
    getBreadcrumb () {
      const { params } = this.$route
      let matched = this.$route.matched.filter(item => {
        if (item.name) {
          // To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
          var toPath = pathToRegexp.compile(item.path)
          item.path = toPath(params)
          return true
        }
      })
      const first = matched[0]
      if (first && first.name !== 'dashboard') {
        matched = [{ path: '/dashboard', meta: { title: '主页', icon: 'yemian-copy' }}].concat(matched)
      }
      this.levelList = matched
    }
  }
}
</script>

<style lang="stylus">
  .app-breadcrumb.el-breadcrumb {
    display: inline-block;
    font-size: 14px;
    line-height: 50px;
    margin-left: 10px;
    .no-redirect {
      color: #97a8be;
      cursor: text;
    }
    .radio-group-color{
      .el-radio-button__orig-radio:checked+.el-radio-button__inner {
        color: #fff;
        background-color: #0bd289;
        border-color: #0bd289;
        -webkit-box-shadow: -1px 0 0 0 #0bd289;
      }
      .el-radio-button:first-child .el-radio-button__inner{
        border-radius 0px
      }
      .el-radio-button:last-child .el-radio-button__inner{
        border-radius 0px
      }
      .el-radio-button__inner:hover {
        color: #FFF;
      }
      .el-radio-button, .el-radio-button__inner{
        margin-right: 10px;
      }
      .el-radio-button__inner {
        border-left: 1px solid #dcdfe6;
        color #fff
        background-color #797676
      }
      .el-button {
        border-radius: 0;
      }
    }
  }
  .nowDate-box {
    display: inline-block;
    width 251px
    height 50px
    line-height 50px
    float right
    color #ffffff
    margin-right 15px
    .blue-btn{
      width 100%
      height 34px
      line-height 34px
      display inline-block
      background-color #0a819c
      border-color #097992
      vertical-align middle
      font-size 14px
      padding-left 10px
    }
  }
</style>
